// pages/building/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    buildingbriefList:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // console.log(options);
    const id = options.id;
    this.buildingbrief(id);
    this.checkCollectionStatus(id);
  },

  async buildingbrief(e){
    const openidRes = await wx.cloud.callFunction({
      name: 'getUserOpenid'
    });
    try {
      const res = await wx.cloud.callFunction({
        name:'buildingbrief',
        data:{
          openid:openidRes.result.openid,
          id:e
        }
      })
      this.setData({
        buildingbriefList:res.result.data
      })
    } catch (error) {
      console.log('云函数调用失败');
    }
  },

  // 初始化页面时调用检查收藏状态
  async checkCollectionStatus(e) {
    const openidRes = await wx.cloud.callFunction({
      name: 'getUserOpenid'
    });
    const queryRes = await wx.cloud.callFunction({
      name: 'selectFunction',
      data: {
        collectionName: 'collection',
        queryCondition: {
          openid: openidRes.result.openid,
          Jid: e
        }
      }
    });
    // console.log(queryRes);
    this.setData({
      isCollected: queryRes.result.total > 0
    });
  },
  // 收藏和取消收藏的逻辑
  async collection(e) {
    // console.log(e.currentTarget.dataset.item);
    const openidRes = await wx.cloud.callFunction({
      name: 'getUserOpenid'
    });
  
    const itemRes = e.currentTarget.dataset.item;
    if (this.data.isCollected) {
      // 已收藏，执行取消收藏逻辑
      const deleteRes = await wx.cloud.callFunction({
        name: 'daleteFuntion',
        data: {
          collectionName: 'collection',
          queryCondition: {
            openid: openidRes.result.openid,
            Jid: itemRes._id
          }
        }
      });
      
      if (deleteRes.result.success) {
        this.setData({ 
          isCollected: false 
        });
        wx.showToast({ 
          title: '已取消收藏', icon: 'success' 
        });
      }
    } else {
      // 未收藏，执行收藏逻辑
      const addRes = await wx.cloud.callFunction({
        name: 'addFunction',
        data: {
          collectionName: 'collection',
          data: {
            openid: openidRes.result.openid,
            Jid: itemRes._id,
            name:itemRes.name,
            image:itemRes.image,
            description:itemRes.description,
            creatTime:this.formatDate(new Date())
          }
        }
      });
      // 更改按钮
      if (addRes.result.success) {
        this.setData({ isCollected: true });
        wx.showToast({ title: '收藏成功', icon: 'success' });
      }
    }
  },

  formatDate(date) {
    const d = new Date(date);
    const year = d.getFullYear();
    const month = (d.getMonth() + 1).toString().padStart(2, '0');
    const day = d.getDate().toString().padStart(2, '0');
    const hours = d.getHours().toString().padStart(2, '0');
    const minutes = d.getMinutes().toString().padStart(2, '0');
    const seconds = d.getSeconds().toString().padStart(2, '0');
    
    return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})